var util = require('../../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ishidden:false,
    mealList:[],
    newArrList:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中...',
    })
    var that=this;
    wx.getStorage({
      key: 'userInfo',
      success: function (res) {
        util.getReq("/app/service/buyServiceList", {
          source: res.data.source,
          uid: res.data.user_id,
        }, function (datas) {
          wx.hideLoading();
          if (datas.code == 0) {
            let mealList = datas.params.mealList;
            let serviceList = datas.params.serviceList;
            let newArrList=[];
            for(let i=0;i<mealList.length;i++){
              newArrList.push({
                title:'套餐服务',
                meal_name: mealList[i].meal_name,
                meal_price: mealList[i].meal_price,
                discount_rate: mealList[i].discount_rate,
                shelf_end_time: util.format(mealList[i].shelf_end_time,'yyyy-MM-dd'),
                meal_id: mealList[i].meal_id,
                discount_price: mealList[i].discount_price,
                meal_type:1,
                menuimg:'/images/crown.png',
              })
            }
            for (let i = 0; i < serviceList.length; i++) {
              newArrList.push({
                title: '单一服务',
                meal_name: serviceList[i].service_name,
                meal_price: serviceList[i].service_price,
                discount_rate: serviceList[i].discount_rate,
                discount_price: serviceList[i].discount_price,
                shelf_end_time: util.format(serviceList[i].shelf_end_time, 'yyyy-MM-dd'),
                meal_id: serviceList[i].service_id,
                meal_type: 2,
                menuimg: '/images/zuanshi.png',
              })
            }
            that.setData({
              newArrList: newArrList,
             
            })

            if (mealList.length <= 0 && serviceList.length<=0) {
              that.setData({
                ishidden: false,
              })
            } else {
              that.setData({
                ishidden: true,
              })
            }
          } else {
            var errMsg = '服务器异常';
            if (datas.desc) {
              errMsg = datas.desc;
            }
            wx.showToast({
              title: errMsg,
            })
          }

        })
      }
    })    
  
  },
  /**
     * 跳转
     */
  navigatoDetail: function (e) {
    let id = e.currentTarget.dataset.id;
    let types = e.currentTarget.dataset.type;
    let title = e.currentTarget.dataset.title;
    let url = '/pages/my/service/service_detail?id=' + id + '&type=' + types + '&title=' + title;
      wx.navigateTo({
        url: url,
      })
  }
  
})